package service;

import dao.db;
import pojo.Consumption;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class consumptionService {
    public static int insert(Consumption consumption) {
        db d = new db();
        String sql = "INSERT INTO hotel.consumption (guest_id, guset_name, room_id, room_type, fee, start_time, end_time, days, comment)\n" +
                "VALUES (" + consumption.getGuestId() +
                ", '" + consumption.getGusetName() +
                "', " + consumption.getRoomId() +
                ", '" + consumption.getRoomType() +
                "', " + consumption.getFee() +
                ", '" + consumption.getStartTime() +
                "', '" + consumption.getEndTime() +
                "', " + consumption.getDays() +
                ", '" + consumption.getComment() +
                "');";
        System.out.println(sql);
        return d.update_execute(sql);
    }

    public static List<Consumption> all() {
        db d = new db();
        String sql = "select * from Consumption";
        ResultSet resultSet = d.executeQuery(sql);
        List<Object> list = db.selectList(new Consumption(), resultSet);
        List<Consumption> list1 = new ArrayList<>();
        list.forEach(i -> {
            list1.add((Consumption) i);
        });
        return list1;
    }

    public static List<Consumption> queryByid(int id) {
        db d = new db();
        String sql = "select * from Consumption where id=" + id;
        ResultSet resultSet = d.executeQuery(sql);
        List<Object> list = db.selectList(new Consumption(), resultSet);
        List<Consumption> list1 = new ArrayList<>();
        list.forEach(i -> {
            list1.add((Consumption) i);
        });
        return list1;
    }

    public static List<Consumption> queryByName(String name) {
        db d = new db();
        String sql = "select * from Consumption where bname like '%" + name + "%'";
        System.out.println(sql);
        ResultSet resultSet = d.executeQuery(sql);
        List<Object> list = db.selectList(new Consumption(), resultSet);
        List<Consumption> list1 = new ArrayList<>();
        list.forEach(i -> {
            list1.add((Consumption) i);
        });
        return list1;
    }

    public static int deleteByid(int id) {
        db d = new db();
        String sql = "delete from Consumption where id=" + id;
        return d.update_execute(sql);
    }

    public static int updateByid(Consumption c) {
        db d = new db();
        String sql = "UPDATE hotel.consumption t\n" +
                "SET t.guest_id   = " + c.getGuestId() +
                ",\n" +
                "    t.guset_name = '" + c.getGusetName() +
                "',\n" +
                "    t.room_type  = '" + c.getRoomType() +
                "',\n" +
                "    t.fee        = " + c.getFee() +
                ",\n" +
                "    t.start_time = '" + c.getStartTime() +
                "',\n" +
                "    t.end_time   = '" + c.getEndTime() +
                "',\n" +
                "    t.days       = " + c.getDays() +
                ",\n" +
                "    t.comment    = '" + c.getComment() +
                "'\n" +
                "WHERE t.id = " + c.getId() +
                ";";
        return d.update_execute(sql);
    }
}
